<?php


namespace app\admin\controller\pz;

use app\common\controller\Adminbase;
use think\Db;
class Pz extends Adminbase
{
    public function index(){
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $business_where[] = ['is_del', '=', 0];
            $business_where[] = ['type', '=', 1];
            $business_where[] = ['status', 'in', [1,2]];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $business_where[] = ['province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $business_where[] = ['city', '=', $city['name']];
                }
                if (isset($filter['name'])) {
                    $business_where[] = ['name', 'like', '%'.$filter['name'].'%'];
                }
            }
            $_list = Db::name('pz')
                ->page($page, $limit)
                ->where($business_where)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k=>$v){
                $member=Db::name('member')->find($v['member_id']);
                $_list[$k]['nickname']=$member['nickname'];
                $_list[$k]['avatarUrl']=$member['avatarUrl'];
                $pz_level=Db::name('pz_level')->find($v['pz_level_id']);
                $_list[$k]['pz_level_name']=$pz_level['name'];
                $_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                $_list[$k]['status_time']=date('Y-m-d H:i:s',$v['status_time']);
            }
            $total = Db::name('pz')
                ->where($business_where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['row']['status']=1;
            $data['row']['type'] = 1;
            $data['row']['create_time'] = time();
            $data['row']['status_time'] = time();
            $stmt = Db::name('pz')->insertGetId($data['row']);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            $area_list = Db::name('area')->where([
                ['id', '<>', 0],
                ['pid', '=', 0]
            ])->select();
            foreach ($area_list as $k => $v) {
                $proviec[$v['id']] = $v['name'];
            }
            $plevel_list = Db::name('pz_level')->where([
                ['is_del', '=', 0],
                ['type', '=', 1]
            ])->select();
            $this->assign([
                'proviec'=>$area_list,
                'plevel_list'=>$plevel_list
            ]);
            return $this->fetch();
        }
    }


    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data = Db::name('pz')
                ->update($data['row']);
            $this->success('操作成功');
        }
        $data = Db::name('pz')->find($id);
        $data['q_img']=Db::name('qualification')->where([
            'pz_id'=>$id
        ])->order('sort')->select();
        $proviec_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        $provice = Db::name('area')->where(['name' => $data['province']])->find();
        $city_list = Db::name('area')->where([
            'pid' => $provice['id']
        ])->select();
        $city = Db::name('area')->where(['name' => $data['city']])->find();
        $area_list = Db::name('area')->where([
            'pid' => $city['id']
        ])->select();
        if ($area_list[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $area_list[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $area_list[] = $v;
            }
            unset($area_list[0]);
        }
        usort($area_list, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        $plevel_list = Db::name('pz_level')->where([
            ['is_del', '=', 0],
            ['type', '=', 1]
        ])->select();

        $this->assign([
            'data' => $data,
            'city_list' => $city_list,
            'proviec' => $proviec_list,
            'area_list' => $area_list,
            'plevel_list'=>$plevel_list
        ]);
        return $this->fetch();
    }

    public function commentList()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
//            $where[] = ['co.reply_id', '=', 0];
            $where[] = ['co.is_del', '=', 0];
            $where[] = ['co.pz_id', '=', $id];
//            $where[] = ['co.type', 'in', [1,2]];
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
//                if (isset($filter['id'])) {
//                    $where[] = ['co.id', '=', $filter['id']];
//                }
//                if (isset($filter['status'])) {
//                    $where[] = ['co.status', '=', $filter['status']];
//                }
//                if (isset($filter['create_time'])) {
//                    $create_time = explode(' - ', $filter['create_time']);
//                    foreach ($create_time as $k => $v) {
//                        $create_time[$k] = strtotime($v);
//                    }
//                    $where[] = ['co.create_time', 'between', [$create_time[0], $create_time[1]]];
//                }
//                if (isset($filter['nickname'])) {
//                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
//                }
//                if (isset($filter['goods_name'])) {
//                    $where[] = ['g.name', 'like', '%' . $filter['goods_name'] . '%'];
//                }
//                if (isset($filter['order_no'])) {
//                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
//                }
            }
            $_list = Db::name('comment')->alias('co')
//                ->join('goods g', 'co.goods_id=g.id')
                ->join('member m', 'co.member_id=m.id')
//                ->join('order o', 'co.order_id=o.id')
                ->where($where)
                ->field('co.*,m.nickname')
                ->page($page, $limit)
                ->order('co.create_time desc')
                ->select();
            $total = Db::name('comment')->alias('co')
//                ->join('order o', 'co.order_id=o.id')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['server_id']==0){
                    $server=Db::name('server2')->find();
                }else{
                    $server=Db::name('server')->find($v['server_id']);
                }
                $_list[$k]['server_name']=$server['name'];
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        //经营方式
//        $operate_type = Db::name('goods')->where([
//            'business_id' => $id,
//            'is_del' => 0
//        ])->order('create_time desc')->select();
//        foreach ($operate_type as $k => $v) {
//            $goods_list2[$v['id']] = $v['name'];
//        }
        $this->assign([
            'pz_id' => $id,
        ]);
        return $this->fetch();
    }

    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('comment')->where('id', $id)->update(['status' => $status])) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    public function replay()
    {
//        if ($this->request->isPost()) {
//            $data = $this->request->Post();
//            $insert_data = array(
//                'content' => $data['reply_content'],
//                'create_time' => time(),
//                'reply_id' => $data['id'],
//                'reply_role'=>3
//            );
//            $comment_id = Db::name('comment')->insertGetId($insert_data);
//            if ($comment_id) {
//                $this->success('操作成功！');
//            } else {
//                $this->error('操作失败！');
//            }
//        }
        $id = $this->request->param('id/d', 0);
        //获取评价消息
        //获取相关图片
        $data = Db::name('comment')
            ->alias('c')
            ->find($id);
        $data['img_list'] = Db::name('comment_img')
            ->where('comment_id', '=', $id)
            ->select();
        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch();
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();
        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function getc_cate2()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }
    public function del()
    {
        $ids = $this->request->param('ids/a', null);
        try {
            foreach ($ids as $id) {
                Db::name('pz')->where('id', $id)->update([
                    'is_del' => 1,
                ]);
            }
        } catch (\Exception $ex) {
            $this->error($ex->getMessage());
        }
        $this->success('删除成功！');
    }

}